package com.zzyl.nursing.service.impl;

import java.util.List;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.domain.NursingLevel;
import com.zzyl.nursing.service.INursingLevelService;

/**
 * 护理等级Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-07
 */
@Service
public class NursingLevelServiceImpl implements INursingLevelService 
{
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    private static final String KEY = "nursingLevel:all";

    /**
     * 查询护理等级
     * 
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id)
    {
        return nursingLevelMapper.selectNursingLevelById(id);
    }

    /**
     * 查询护理等级列表
     * 
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelList(NursingLevel nursingLevel)
    {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel)
    {
        nursingLevel.setCreateTime(DateUtils.getNowDate());
        int count = nursingLevelMapper.insertNursingLevel(nursingLevel);
        if(count>0){
            deleteCache();
        }
        return count;
    }

    /**
     * 修改护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel)
    {
        nursingLevel.setUpdateTime(DateUtils.getNowDate());
        int count = nursingLevelMapper.updateNursingLevel(nursingLevel);
        if(count>0){
            deleteCache();
        }
        return count;
    }

    /**
     * 批量删除护理等级
     * 
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids)
    {
        int count = nursingLevelMapper.deleteNursingLevelByIds(ids);
        if(count>0){
            deleteCache();
        }
        return count;
    }

    /**
     * 删除护理等级信息
     * 
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id)
    {
        int count = nursingLevelMapper.deleteNursingLevelById(id);
        if(count>0){
            deleteCache();
        }
        return count;
    }

    @Override
    public List<NursingLevelVo> listAll() {
        //1、查询Redis缓存，如果有数据，直接返回
        List<NursingLevelVo> nursingLevelVos = (List<NursingLevelVo>) redisTemplate.opsForValue().get(KEY);
        if(nursingLevelVos!=null && nursingLevelVos.size()>0){
            return nursingLevelVos;
        }

        //2、如果没数据，则查询数据库
        nursingLevelVos = nursingLevelMapper.listAll();

        //3、将查询的数据存入到Redis缓存
        if(nursingLevelVos!=null && nursingLevelVos.size()>0){
            redisTemplate.opsForValue().set(KEY,nursingLevelVos);

        }

        //4、返回数据
        return nursingLevelVos;
    }

    /***
     * 删除缓存方法
     */
    public void deleteCache(){
        redisTemplate.delete(KEY);
    }


}
